package cn.schoolwow.quickflow.service.quickFlow.getData;

import cn.schoolwow.quickflow.domain.FlowConfig;
import cn.schoolwow.quickflow.util.QuickFlowUtil;

import java.util.Map;

public class QuickFlowGetDataOperationImpl implements QuickFlowGetDataOperation {
    private FlowConfig flowConfig;

    public QuickFlowGetDataOperationImpl(FlowConfig flowConfig) {
        this.flowConfig = flowConfig;
    }

    @Override
    public Object getContextThreadLocalData(String key) {
        Map<String,Object> map = flowConfig.flowConfigDataContext.threadLocalDataMap.get();
        if(null!=map){
            return map.get(key);
        }
        return null;
    }

    @Override
    public <T> T getContextThreadLocalData(String key, Class<T> clazz) {
        Object value = getContextThreadLocalData(key);
        return QuickFlowUtil.convertToClassType(value, clazz);
    }

    @Override
    public <T> T getContextThreadLocalInstanceData(Class<T> clazz) {
        String key = clazz.getName();
        return getContextThreadLocalData(key, clazz);
    }

    @Override
    public Object getContextData(String key) {
        return flowConfig.flowConfigDataContext.dataMap.get(key);
    }

    @Override
    public <T> T getContextData(String key, Class<T> clazz) {
        Object value = getContextData(key);
        return QuickFlowUtil.convertToClassType(value, clazz);
    }

    @Override
    public <T> T getContextInstanceData(Class<T> clazz) {
        String key = clazz.getName();
        return getContextData(key, clazz);
    }

    @Override
    public Map<String, Object> getContextThreadLocalDataMap() {
        Map<String,Object> map = flowConfig.flowConfigDataContext.threadLocalDataMap.get();
        return map;
    }

    @Override
    public Map<String, Object> getContextDataMap() {
        return flowConfig.flowConfigDataContext.dataMap;
    }

}
